package RKUjava.math;
import java.util.BitSet;

/** Gray encoding and decoding of binary strings. */
public class RKUGray {

  /** Gray decoding of an integer array containing zeros and ones.
      @param x The integer array to decode.
      @return The decoding of the integer array.
  */
  public static int decode(int[] x)
    {
      int ones = 0;
      int intval = 0;
      int i;

      for (i=0;i<x.length;i++) {
	if (x[i]==1)
	  ones++;
	intval += (ones%2)*(1<<(x.length-1-i));
      }

      return intval;
    }

  /** Gray decoding of an boolean array.
      @param x The boolean array to decode.
      @return The decoding of the boolean array.
  */
  public static int decode(boolean[] x)
    {
      int ones = 0;
      int intval = 0;
      int i;

      for (i=0;i<x.length;i++) {
	if (x[i])
	  ones++;
	intval += (ones%2)*(1<<(x.length-1-i));
      }

      return intval;
    }

  /** Gray decoding of an boolean array.
      @param x The boolean array to decode.
      @return The decoding of the boolean array.
  */
  public synchronized static int decode(BitSet x)
    {
      int ones = 0;
      int intval = 0;
      int i;

      for (i=0;i<x.size();i++) {
	if (x.get(i))
	  ones++;
	intval += (ones%2)*(1<<(x.size()-1-i));
      }

      return intval;
    }

  /** NOT IMPLEMENTED YET. 
      @return null
  */
  public static int[] encodeAsIntArray(int x)
    {
      return null;
    }

  /** NOT IMPLEMENTED YET. 
      @return null
  */
  public static boolean[] encodeAsBooleanArray(int x)
    {
      return null;
    }

  /** NOT IMPLEMENTED YET.
      @return null
  */
  public static BitSet encodeAsBitSet(int x)
    {
      return null;
    }




}
